package ru.lukyanets.transmission.client.util;

import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.StageStyle;
import javafx.stage.Window;

import java.util.Optional;

/**
 * @author slukyanets
 */
public final class DialogUtil {
    public static final ButtonType YES = new ButtonType(
            LocalizationUtil.getString("dialog.yes.button"), ButtonBar.ButtonData.YES);
    public static final ButtonType CANCEL = new ButtonType(
            LocalizationUtil.getString("dialog.cancel.button"), ButtonBar.ButtonData.CANCEL_CLOSE);

    public static Node buildInputContent(final Control inputControl, String message) {
        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setMaxWidth(Double.MAX_VALUE);

        if (message != null && !message.isEmpty()) {
            Label label = new Label(message);
            GridPane.setHgrow(label, Priority.NEVER);
            grid.add(label, 0, 0);
        }

        if (inputControl != null) {
            inputControl.setMaxWidth(Double.MAX_VALUE);
            GridPane.setHgrow(inputControl, Priority.ALWAYS);
            grid.add(inputControl, 1, 0);
        }

        return grid;
    }

    public static void showError(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setContentText(message);
        alert.showAndWait();
        alert.initStyle(StageStyle.UTILITY);
    }

    public static boolean showConfirm(Window window, String title, String content) {
        Dialog<ButtonType> dialog = new Dialog<>();
        dialog.initOwner(window);
        dialog.setTitle(title);
        dialog.setResizable(false);
        dialog.initStyle(StageStyle.UTILITY);

        dialog.getDialogPane().getButtonTypes().addAll(YES, CANCEL);
        dialog.setContentText(content);

        Optional<ButtonType> result = dialog.showAndWait();
        return result.get() == YES;
    }
}
